/*
 * Copyright (c) ycq_work@163.com
 */

package com.mxg.blog.controller.articleAdmin;

import com.mxg.blog.common.CqLogger;
import com.mxg.blog.common.ResultCode;
import com.mxg.blog.entity.Article;
import com.mxg.blog.service.ArticleCategoryService;
import com.mxg.blog.service.ArticleService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/5/29 6:32
 * 获取文章根据文章Id
 */
@Controller
public class GetArticle {
    CqLogger logger = CqLogger.getInstance();
    private final ArticleService articleService;
    private final ArticleCategoryService articleCategoryService;
    public GetArticle(ArticleService articleService, ArticleCategoryService articleCategoryService) {
        this.articleService = articleService;
        this.articleCategoryService = articleCategoryService;
    }

    @PostMapping("/api/getArticle")
    public ResponseEntity<Map<String, Object>> getArticle(@RequestBody Map<String, String> requestMap) {
        Map<String, Object> resultMap = new HashMap<>();
        logger.print(requestMap);
        String articleIdString = requestMap.get("articleId");

        Integer articleId = new Integer(articleIdString);

        logger.print(articleId);

        Article article = articleService.getArticleByArticleId(articleId);

        if(article != null){
            resultMap.put(ResultCode.code, ResultCode.SUCCESS_CODE);
            resultMap.put(ResultCode.title, article.getArticleTitle());
            resultMap.put(ResultCode.text, article.getArticleContentMd());
            resultMap.put(ResultCode.imageUrl, article.getArticleImages());
            resultMap.put(ResultCode.labelValue, article.getArticleTags());
            resultMap.put(ResultCode.textarea, article.getArticleExcerpt());
            resultMap.put(ResultCode.articleCategory, articleCategoryService.getArticleCategoryNameById(article.getArticleCategoryId()));
            resultMap.put(ResultCode.message, ResultCode.SUCCESS_MESSAGE);
            logger.print(resultMap);
            return ResponseEntity.ok(resultMap);
        }else {
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_HAVE_NO_THIS_ARTICLE);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_HAVE_NO_THIS_ARTICLE);
        }

        return ResponseEntity.ok(resultMap);
    }
}